package ikar.view.components;

import ikar.nrd.fillingref.generator.CoreUtils;
import ikar.nrd.model.FillingRef;

import javax.swing.tree.DefaultMutableTreeNode;

import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.panel.Panel;

public class FillingRefDataBaseScriptPanel extends Panel {

	private final String insertFillingRef = "   INSERT nrd.filling_ref"
			+ "(field_id, pattern_id, name, reference_id, parent_field_id, xpath, modified_by, modified_date, is_enabled, plate_code, is_mandatory, short_form_enabled, is_modifiable)"
			+ "\nVALUES ((select max(field_id)+1 from nrd.filling_ref ), %s,N'%s',"
			+ "NULL, (select field_id from nrd.filling_ref where pattern_id = %s and plate_code = '%s'),"
			+ " N'%s', N'admin', getdate(), 1, N'%s', 1, 1, 1)\n";

	private final String insertStyleRef = "   INSERT INTO [nrd].[style_ref]([id],[field_id],[field_group_id],[level],[sublevel],[field_type],[modified_by],[modified_date])\n" +
			"VALUES((select max(id)+1 from [nrd].[style_ref]), (select field_id from nrd.filling_ref where pattern_id = %s and plate_code = '%s' )" +
			",(select field_group_id from nrd.field_groups where code = '%s'),%s,%s,'%s','I.KAR',GETDATE())\n";

	public FillingRefDataBaseScriptPanel(String id, DefaultMutableTreeNode rootNode) {
		super(id);
		add(new MultiLineLabel("multiLineLabel", getNodesIerarchy(rootNode).toString()).setEscapeModelStrings(false));
	}

	private static final long serialVersionUID = 1L;

	private StringBuilder getNodesIerarchy(DefaultMutableTreeNode rootNode) {
		StringBuilder strBuilder = new StringBuilder();

		for (int i = 0; i < rootNode.getChildCount(); i++) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) rootNode.getChildAt(i);
			FillingRef fillRef = ((FillingRef) node.getUserObject());

			if (rootNode.getChildAt(i).getChildCount() > 0) {
				if (fillRef.isSelected()) {
					strBuilder.append(getFillRefInsertQuery(fillRef));
					if (fillRef.isStyleRefNeed()) {
						strBuilder.append(getStyleRefInsertQuery(fillRef));
					}
					strBuilder.append("--");
				}
				strBuilder.append("\n");
				strBuilder.append(getNodesIerarchy(node));
				strBuilder.append("\n");
			} else {
				if (fillRef.isSelected()) {
					strBuilder.append(getFillRefInsertQuery(fillRef));
					if (fillRef.isStyleRefNeed()) {
						strBuilder.append(getStyleRefInsertQuery(fillRef));
					}
					strBuilder.append("--");

				}
				strBuilder.append("\n");
			}
		}
		return strBuilder;
	}

	private String getFillRefInsertQuery(FillingRef fillRef) {
		return String.format(insertFillingRef, FillingRef.getPatternId(), getFillRefsName(fillRef), FillingRef.getPatternId(), CoreUtils.getColredValue(fillRef.getParentPlateCode(), "green", 3), fillRef.getXpath(), CoreUtils.getColredValue(fillRef.getPlateCode(), "green", 3));
	}

	private String getStyleRefInsertQuery(FillingRef fillRef) {
		return String.format(insertStyleRef, FillingRef.getPatternId(), CoreUtils.getColredValue(fillRef.getPlateCode(), "blue", 3), fillRef.getFieldsGroup(), fillRef.getLevel(), fillRef.getSubLevel(), fillRef.getFieldType());
	}

	private String getFillRefsName(FillingRef fillRef) {
		if (fillRef.getName() == null || fillRef.getName().length() == 0) {
			return "<font color=red size=3>UNKNOWN</font>";
		} else {
			return fillRef.getName();
		}
	}

}
